package org.fast.develop.logicflow.management.core;

import org.fast.develop.logicflow.management.core.event.LogicEvent;
import org.fast.develop.logicflow.management.core.listener.LogicListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-12-21 03:30
 * @desc：
 */
public class EventBus {


    Map<Class, List<LogicListener>> logicListeners = new HashMap<>();

    public void register(Class<? extends LogicEvent> logicEventClass, LogicListener logicListener){
        List<LogicListener> logicListenerList = logicListeners.get(logicEventClass);
        if(logicListenerList == null){
            logicListenerList = new ArrayList<>();
            logicListeners.put(logicEventClass, logicListenerList);
        }
        logicListenerList.add(logicListener);
    }

    public void publishEvent(LogicEvent event) {
        List<LogicListener> listeners = logicListeners.get(event.getClass());
        if(listeners != null){
            for (LogicListener listener : listeners) {
                listener.onEvent(event);
            }
        }

    }

}
